<?php

declare(strict_types=1);

namespace app\common\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class User extends Model
{
    /**
     * 更新统计
     *
     * @param integer $correct 回答正确的数量
     * @param integer $num  答题数量
     * @return void
     * @author Ymob
     * @datetime 2021-04-25 09:24:54
     */
    public function anwser($correct = 0, $num = 1)
    {
        $this->num += $num;
        $this->correct += $correct;
        $this->save();
    }

    /**
     * 答题准确率
     *
     * @return void
     * @author Ymob
     * @datetime 2021-04-25 10:01:41
     */
    public function getRateAttr()
    {
        if ($this->num) {
            $rate = number_format($this->correct / $this->num * 100, 2);
        } else {
            $rate = 0;
        }
        return $rate;
    }
}
